package com.hanyxx.io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * @author layman
 * @date 2021/3/14
 */
public class Demo10 {
    public static void main(String[] args) throws IOException {
        Hero hero = new Hero("葫芦娃",3);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("hero.txt"));
        // 写出对象
        oos.writeObject(hero);
        oos.close();
    }
}
class Hero implements Serializable{
    public String name;
    public Integer age;
    // 被transient修饰的变量不能被序列化transient
    public transient String address;
    // 被static修饰的变量不能被序列化
    public static String mail;

    public Hero(String name,Integer age){
        this.name = name;
        this.age = age;
        this.address = "中国香港";
        this.mail = "layman@111.com";
    }

    @Override
    public String toString() {
        return "Hero{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                ", mail='" + mail + '\'' +
                '}';
    }
}
